import {showToast, showToastError, showToastSuccess} from "../../../utils/tool";
import {checkLogin, toLogin} from "../../../service/user";
import {goPay} from "../../../service/order";
import goodsCartService from "../../../service/goodsCart";
import {simpleGet} from "../../../utils/request";

const app = getApp()
Component({
    options:{
        styleIsolation: 'apply-shared',
    },
    properties: {
        show:{type:Boolean, value:false},
        goods:{
            type:[Object],value:null,
            observer(data){
                this.setData({
                    _goods:data
                })
                if (data){
                    simpleGet('/goods/skus', {goods_id:data.id}).then(r => {
                        data.skus = r.skus
                        this.setData({
                            _goods:data
                        })
                    })
                }

            }
        },

    },
    data: {
        checked: {
            sku:null,	// 选中的sku
            specArr:{},
            num:0,	// 购买数量,
            specValueStr:'',	// 将当前sku的规格值拼接成字符串
            _goods:null,
        },
        imageUrl:app.globalData.imageUrl
    },
    observers:{
        'checked.sku, checked.num':function (sku, num) {
            let total = 0
            if (sku){
                total = sku.price * num
                this.setData({
                    total
                })
            }
        }
    },
    methods: {
        specSelect(e) {
            const {checked, _goods} = this.data
            const {specId, specValueId} = e.currentTarget.dataset

            // 更新选中规格
            if(!checked.spec){
                checked.spec = {}
            }
            checked.spec[specId] = specValueId

            const ssp = checked.spec // 将当前选中的规格ID键值对集合赋值到局部变量
            // 根据选中的规格更新sku
            const sku = _goods.skus.find(item => {
                checked.specValueStr = ''	// 置空
                // 使用every进行匹配，只要sku中有一个规格值跟规格名不配就代表匹配失败
                const relCheckRes = item.specRels.every(rel => {
                    if(ssp[rel.spec_id] == rel.spec_value_id){
                        checked.specValueStr += rel.spec_value + ' '
                        return true
                    }else{
                        return false
                    }

                })
                // 如果都匹配到了会返回true，外层find将不再往下执行
                return relCheckRes
            })
            // 赋值当前选中的sku
            if (sku){
                checked.sku = sku
                checked.num = sku.num
            }
            this.setData({
                checked
            })
        },

        numChange(e){
            const {value} = e.detail
            this.setData({
                [`checked.num`]: value
            })
        },
        btnClick(e){

            console.log("checkLogin", checkLogin())
            // 登录验证
            if (!checkLogin()) {
                toLogin()
                return;
            }

            const {type} = e.currentTarget.dataset
            const {_goods, checked} = this.data
            if (type == 'cart'){    // 加入购物车
                if (!checked.sku){
                    showToastError("请选择规格")
                    return;
                }
                if (checked.num > 0){
                    // 添加购物车
                    goodsCartService.add({
                        goods_id:goods.id,
                        goods_sku_id:checked.sku.id,
                        num: checked.num,
                    })
                }
            }else if (type=='buy'){ // 去购买
                _goods.sku = checked.sku
                _goods.num = checked.num
                // 去下单页面
                goPay({
                    create_type:'goods',
                    order_type:'normal',
                    goodsUl:[_goods]
                })
            }
        },

    }
});
